package org.jeecg.modules.friends.service.impl;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.jeecg.modules.friends.store.entity.BusinessDictionaryEntity;
import org.jeecg.modules.friends.store.mapper.BusinessDictionaryMapper;
import org.jeecg.modules.friends.store.wrapper.BusinessDictionaryQuery;
import org.jeecg.modules.friends.store.wrapper.BusinessDictionaryUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author
 * Created on 2022-09-26
 */
@Slf4j
@Service
public class LocalCacheDictionary {
    @Autowired
    private BusinessDictionaryMapper businessDictionaryMapper;
    private LoadingCache<Integer, String> loadingCache = CacheBuilder
            .newBuilder()
            .maximumSize(Long.MAX_VALUE)
            .refreshAfterWrite(15, TimeUnit.SECONDS) // 刷新时间
            .expireAfterWrite(2, TimeUnit.MINUTES) // 过期时间
            .build(CacheLoader.asyncReloading(new CacheLoader<Integer, String>() {
                @Override
                public String load(Integer key) {
                    BusinessDictionaryEntity dictionaryEntity =
                            businessDictionaryMapper.findOne(
                                    new BusinessDictionaryQuery()
                                            .select
                                            .content()
                                            .end()
                                            .where.type().eq(key)
                                            .end()
                            );

                    if (dictionaryEntity == null) {
                        return "";
                    }
                    return dictionaryEntity.getContent();
                }
            }, Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2)));

    public String getContentDefault(Integer type, String defaultStr) {
        try {
            return StrUtil.blankToDefault(loadingCache.get(type), defaultStr);
        } catch (ExecutionException e) {
            log.error("getContent ", e);
            return defaultStr;
        }
    }

    public String getContentDefaultNoCache(Integer type, String defaultStr) {
        try {
            BusinessDictionaryEntity dictionaryEntity =
                    businessDictionaryMapper.findOne(
                            new BusinessDictionaryQuery()
                                    .select
                                    .content()
                                    .end()
                                    .where.type().eq(type)
                                    .end()
                    );

            if (dictionaryEntity == null) {
                return defaultStr;
            }
            return dictionaryEntity.getContent();
        } catch (Exception e) {
            log.error("getContent ", e);
            return defaultStr;
        }
    }

    public void update(Integer type, String content) {
        if (type == null || StrUtil.isEmpty(content)) {
            return;
        }
        businessDictionaryMapper.updateBy(
                new BusinessDictionaryUpdate()
                        .set.content().is(content)
                        .end()
                        .where
                        .type().eq(type)
                        .end()
        );
    }
}
